//****--2009.2.19  重新整理添加 by ddd ---***
//较常用到的方法都列到了前面

//-----textarea maxlength限制-------
//使用说明:
//<textarea name="content"  cols="40"  rows="10" onpropertychange="checkMaxLen(this,10,0)" oninput="checkMaxLen(this,10,0)"  onclick="gz(this); return false;" >评论描述字数不得超过10字。</textarea>
function gz(el) {
    el.value = "";
    el.onclick = null;
}

function checkMaxLen(obj, maxlength, num,str) {
    str = str ||"对不起，您输入的字数已超过"+maxlength+"个字，您的文字将被截取。"
    var oldValue = new Array();
    if (obj.value.length > maxlength) {
        alert(str);
        obj.value = obj.value.substring(0, maxlength);
        oldValue[num] = obj.value;
        return false;
    } else {
        oldValue[num] = obj.value;
    }
}


//------判断是否有中文---------
function havaChinese(name, str) {
        str = str || "信息中不允许有汉字。";
        var reg = /[\u0391-\uFFE5]/g;
        if (reg.test(name.value)) {
            alert(str);
            name.focus();
            return false;
        }
        return true;
    }


//-------判断是否有sql非法字符--------
function isSqlCharacter(name,str){
    str = str||"您提交的信息中有非法字符。" ;
//    var namevalue = name.value.toLowerCase() ;//把所有的字母转换成小写 ;
//    alert("namevalue="+namevalue) ;
    var re=/(select|insert|delete|drop|update|truncate|exec|'|"|%)/gi;
    if(re.test(name.value)){
        alert(str);
        name.focus() ;
        return true ;
    }
       return false ;
}

//-------判断是否有特殊字符-------
function isSpecialCharacters(name,str){
    str = str||"提交信息中不允许有特殊字符。";
   var re=/(#|@|\^|%|\$|&|\*|~)/gi;
    if(re.test(name.value)){
        alert(str) ;
        name.focus() ;
        return true ;
    }
    return false ;
}

//-------判断是否有空格-------------
function notSpace(name, strs) {
    strs = strs || "提交信息中不允许有空格。";
    if (contain(name.value, " 　")) {
        alert(strs);
        name.focus();
        return true;
    }
    return false;
}
// ------字符串包含测试函数--------
function contain(name, charset)
{
    var i;
    for (i = 0; i < charset.length; i++)
        if (name.indexOf(charset.charAt(i)) >= 0)
            return true;
    return false;
}


//-------通行证 验证-------
function notAccount(name, str) {
     str = str || "请填写正确的通行证账号。";
//    var reg = /^[a-zA-Z0-9]{6,16}$/;
    var reg = /^[a-z]{1}[a-z0-9]{5,15}$/;
    if (trim(name.value) == "" ||!reg.test(name.value)) {
        alert(str);
        name.focus();
        return true;
    }
    return false;
}

//-------身份证 验证-------
function notIdCard(name, str) {
    str = str || "请填写正确的身份证号码。";
    var reg = /^(\d{15}|\d{17}[\d,x,X])$/;
    if (trim(name.value) == "" ||!reg.test(name.value)) {
        alert(str);
        name.focus();
        return true;
    }
    return false;
}


  //-------固定号码 验证-------
/*function notFixPhone(name, str) {
    str = str || "请填写正确的电话号码。";
    var reg = /(\d{2,5}-\d{7,8}(-\d{1,})?)|(13\d{9})|(159\d{8})|(158\d{8})/;
    if (trim(name.value) == "" ||!reg.test(name.value)) {
        alert(str);
        name.focus();
        return true;
    }
    return false;
}
*/

function notFixPhone(name, str) {
    str = str || "请填写正确的电话号码。";
    var reg = /^(\d{2,5}-\d{7,8}(-\d{1,})?)|(13\d{9})|(15\d{9})|(18\d{9})$/;
    if (trim(name.value) == "" || !reg.test(name.value)) {
        alert(str);
        name.focus();
        return true;
    }
    return false;
}



//-------网址，MSN 验证-------
function notSrc(name, str) {
    str = str || "请填写正确的地址。";
    var regex = /(http:\/\/)?([\w-]+\.)+[\w-]+(\/[\w-   \.\/?%&=]*)?/;
    var src = name.value ;
    var result = regex.test(src);
    if (trim(src) == "" ||result == false) {
        alert(str);
        name.focus();
        return true;
    }
    return false;
}
//---------年龄  验证--------------
function notAge(name, str) {
    str = str || "请填写正确的年龄。";
    var age = name.value ;
    if (trim(age) == "" ||isNaN(trim(age)) || age < 1 || age > 150) {
        alert(str);
        name.focus();
        return true;
    }
    return false;

}

//-------邮编 验证-------
function notPost(name,str) {
    str = str ||"请输入正确的邮政编码。" ;
    if (trim(name.value) == ""||trim(name.value).length != 6 || isNaN(trim(name.value))) {
        alert(str);
        name.focus();
        return true;
    }
    return false;
}



//-------是否汉字-------
function notChinese(name,str) {
    str = str || "你输入的不全是汉字。";
    var reg = /^[\u4E00-\u9FA5]*$/;
    if (trim(name.value) == ""||!reg.test(name)) {
        alert(str);
        name.focus();
        return false;
    }
    return true;
}


//-------邮箱验证-------
function notEmail(name, strs) {
    strs = strs || "请填写正确的邮件地址。";
    var str,re;
    re = /([\w|-]+@[\w|-]+\.[\w|-]+)(\.?[\w|-]*)(\.?[\w|-]*)/i;

    re.exec(trim(name.value));
    if (RegExp.$3 != "" && RegExp.$3 != "." && RegExp.$2 != ".")
        str = RegExp.$1 + RegExp.$2 + RegExp.$3;
    else
        if (RegExp.$2 != "" && RegExp.$2 != ".")
            str = RegExp.$1 + RegExp.$2;
        else
            str = RegExp.$1;
    if (trim(name.value) == ""||str != trim(name.value))
    {
        alert(strs);
        name.focus();
        return true;
    }
    return false;
}

//-------输入框文字上传验证  min上限   max下限(字节计算)-------
function notStrArea(name, min, max, str2) {
    var str = name.value;
    len = 0;
    for (i = 0; i < str.length; i++) {
        iCode = str.charCodeAt(i);
        if ((iCode >= 0 && iCode <= 255) || (iCode >= 0xff61 && iCode <= 0xff9f)) {
            len += 0.5;
            //english
        } else {
            len += 1;
        }
    }
    if (trim(str) == ""||len < min || len > max) {
        alert(str2);
        name.focus();
        return true;
    }
    return false;
}

//-------输入框文字上传验证 最多字数限制(字节计算)-------
function notStrMax(name, max, str2) {
    var str = name.value;
    len = 0;
    for (i = 0; i < str.length; i++) {
        iCode = str.charCodeAt(i);
        if ((iCode >= 0 && iCode <= 255) || (iCode >= 0xff61 && iCode <= 0xff9f)) {
            len += 0.5;
        } else {
            len += 1;
        }
    }
    if (trim(str) == ""||len > max) {
        alert(str2);
        name.focus();
        return true;
    }
    return false;
}

//-------只判断 是否数字 -------
function notNumber(name, str) {
    if (trim(name.value) == ""||isNaN(trim(name.value))) {
        alert(str);
        name.focus();
        return true;
    }
    return false;
}


//-------是否数字 未填写默认0-------
function notNumber0(name, str) {
    if (trim(name.value) == "") {
        name.value = 0;
        return false;
    }
    else if (isNaN(trim(name.value))) {
        alert(str);
        name.focus();
        return true;
    }
    return false;
}


//-------空值 验证-------
function isNull(name, str) {
    if (trim(name.value) == "") {
        alert(str);
        name.focus();
        return true;
    }
    return false;
}

//-------是否字母------
function CheckAlphabet(Str) {
    var flag = true;
    var Strlength = Str.length;
    for (var i = 0; i < Strlength; i++) {
        if (Str.charCodeAt(i) < 65 || (90 < Str.charCodeAt(i) && Str.charCodeAt(i) < 97) || Str.charCodeAt(i) > 122) {
            flag = false;
            break;
        }
    }

    return flag;
}
//---------是否含有数字 ---------
function checkInputValue(inputvalue) {
    var i,len,str;
    str = inputvalue;
    len = str.length;
    str = str.toUpperCase();
    for (i = 0; i < len; i++)
    {
        c = str.charAt(i);
        if (c >= "0" && c <= "9")
        {
            return true;
        }
    }
    return false;
}


//---------是否是带小数点的数字---------
function notFloat(name, str) {
    var s = parseFloat(trim(name.value));
    if (isNaN(s)) {
        alert(str);
        name.focus();
        return true;
    }
    else
        return false;
}

//---------检查输入的值，小数点后的位数---------
function checknumber(value, num, str) {
    if (value != "") {
        if (value.indexOf(".") > 0) {
            var temp = value.length - (value.indexOf(".") + 1);
            if (temp > num) {
                alert(str + "的小数点后只能有" + num + "位！");
                return true;
            }
        }
    }
    return false;
}


//---------是否下划线---------
function CheckLine(Str) {
    var flag = true;
    var Strlength = Str.length;
    for (var i = 0; i < Strlength; i++) {
        if (Str.charCodeAt(i) != 95) {
            flag = false;
            break;
        }
    }
    return flag;
}
//-------是否由字母或数字组成------
function notChinese(obj, alertValue) {
    var objValue = obj.value;
    for (var i = 0; i < objValue.length; i++) {
        var temtchar = objValue.charAt(i);
        var Digit = CheckDigit(temtchar);
        var Alphabet = CheckAlphabet(temtchar);

        if (!Digit && !Alphabet) {
            alert(alertValue + "必须是由字母或数字组成!");
            return true;
        }
    }

    return false;
}

//-------必须是由字母、数字、下划线组成!-----------
function notValid(obj)
{
    var objValue = obj.value;
    for (var i = 0; i < objValue.length; i++) {
        var temtchar = objValue.charAt(i);
        var Digit = CheckDigit(temtchar);
        var Alphabet = CheckAlphabet(temtchar);
        var line = CheckLine(temtchar);
        if (!Digit && !Alphabet && !line) {
            alert("请确认您输入的内容必须是由字母、数字、下划线组成!");
            return true;
        }
    }
    return false;
}

//-------是否合法年月  格式为2003-01-01  -------
function notNYR2(name, str) {
    if (trim(name.value).length == 0) {
        return false;
    }
    if (trim(name.value).length != 10) {
        alert(str + "的格式不对，格式应该如此：2003-01-01");
        name.focus();
        return true;
    }
    if (trim(name.value).substring(4, 5) != "-") {
        alert(str + "的年与月份之间应该有连接号,请核对!");
        name.focus();
        return true;
    }
    if (trim(name.value).substring(5, 7) > 12) {
        alert(str + "的月份不能大于12,请核对!");
        name.focus();
        return true;
    }
    if (trim(name.value).substring(7, 8) != "-") {
        alert(str + "的月份与日期之间应该有连接号,请核对!");
        name.focus();
        return true;
    }
    if (trim(name.value).substring(8, 9) > 31) {
        alert(str + "的日期不能大于31,请核对!");
        name.focus();
        return true;
    }
    return false;
}


//----------长度是否对 by lyj ------------
function notLength(name, length, str) {
    if (trim(name.value).length != length) {
        alert(str);
        name.focus();
        return true;
    }
    return false;
}

//-----------固定长度的数字------------
function notLandN(name, length, str) {
    if (trim(name.value).length != length || isNaN(trim(name.value))) {
        alert(str);
        name.focus();
        return true;
    }
    return false;
}

//--------------是否合法年月 by lyj----------------
function notNY1(name, str) {
    if (trim(name.value).length != 6 || isNaN(trim(name.value))) {
        alert(str);
        name.focus();
        return true;
    }
    if (trim(name.value).substring(4, 6) > 12) {
        alert("月份不能大于12,请核对!");
        name.focus();
        return true;
    }
    return false;
}
//-----------是否合法年月 日 by wkh--------------
function notNYR(name, str) {
    if (trim(name.value) == "" || trim(name.value).length != 8 || isNaN(trim(name.value))) {
        alert(str);
        name.focus();
        return true;
    }
    if (trim(name.value).substring(4, 6) > 12) {
        alert("月份不能大于12,请核对!");
        name.focus();
        return true;
    }
    if (trim(name.value).substring(6, 8) > 31) {
        alert("日期不能大于31,请核对!");
        name.focus();
        return true;
    }
    return false;
}

//------------是否合法年月 日 by wkh----------------
function notNYR1(name, str) {
    if (trim(name.value).length != 8 || isNaN(trim(name.value))) {
        alert(str);
        name.focus();
        return true;
    }
    if (trim(name.value).substring(4, 6) > 12) {
        alert("月份不能大于12,请核对!");
        name.focus();
        return true;
    }
    if (trim(name.value).substring(6, 8) > 31) {
        alert("日期不能大于31,请核对!");
        name.focus();
        return true;
    }
    return false;
}


//--------汉字校验--------
function checkIsChinese(str)
{
    //如果值为空，通过校验
    if (str == "")
        return true;
    var pattern = /^([\u4E00-\u9FA5]|[\uFE30-\uFFA0])*$/gi;
    if (pattern.test(str))
    {
        alert("你输入的不全是汉字。");
        return false;
    }
    else
        return true;
}
//--------是否整形数字 ----------
function isIntNumber(name, str) {
    if (isNaN(trim(name.value))) {
    	layer.msg(str,1, {type:3,shade: [0.2, '#ccc']});
        name.focus();
        return true;
    }
    var s = parseInt(trim(name.value));
    if (trim(name.value) != s) {
    	layer.msg(str,1, {type:3,shade: [0.2, '#ccc']});
        name.focus();
        return true;
    }
    else
        return false;
}

//--------是否整形数字--------------
function notIntNumber(name, str) {
    if (isNaN(trim(name.value))) {
        alert(str);
        name.focus();
        return true;
    }

    if (name.value.indexOf('.') >= 0) {
        alert(str);
        name.focus();
        return true;
    }

    var s = parseInt(trim(name.value));
    if (trim(name.value) != s) {
        alert(str);
        name.focus();
        return true;
    }
    else
        return false;
}


function trim(str)
{
    var len;
    var i,j;
    len = str.length;
    i = 0;
    j = 0;
    while (j < len)
    {
        if (str.charAt(i) == " ")
            i++;
        else
            break;
        j++;
    }
    str = str.substr(i, len - i);
    len = str.length;
    i = len - 1;
    j = len - 1;
    while (j > 0)
    {
        if (str.charAt(i) == " ")
            i--;
        else
            break;
        j--;
    }
    str = str.substr(0, j + 1);
    return str;
}


function isStringEmputy(str)
{
    var s;
    s = trim(str);
    alert("s="+s) ;
    str = s;
    alert("str="+str) ;
    if (s.length < 1)
        return true;

    return false;
}

function replace(str1, c1, c2)
{
    var len,i,c,str;
    str = "";
    len = str1.length;
    for (i = 0; i < len; i++)
    {
        c = str1.charAt(i);
        if (c == c1)
            c = c2;
        str += c;
    }
    return str;
}


function toSafe(str)
{
    var re;
    re = /'/g;
    str = str.replace(re, "＇");
    re = /\)/g;
    str = str.replace(re, "）");
    re = /\(/g;
    str = str.replace(re, "（");
    re = /%/g;
    str = str.replace(re, "％");
    re = /\[/;
    str = str.replace(re, "［");
    re = /\]/;
    str = str.replace(re, "］");
    return str;
}

function makeToSafe(formName)
{
    var i,form;
    form = eval(formName);
    for (i = 0; i < form.elements.length; i++)
    {
        if (form.elements[i].type == "text" || form.elements[i].type == "textarea")
        {
            form.elements[i].value = toSafe(form.elements[i].value);
        }
    }
}

//-------登录名必须是由字母、数字、下划线等组成!-----------
function notValidName(obj)
{
    var objValue = obj.value;
    for (var i = 0; i < objValue.length; i++) {
        var temtchar = objValue.charAt(i);
        // alert(temtchar+":"+temtchar.charCodeAt(0));
        var Digit = CheckDigit(temtchar);
        var Alphabet = CheckAlphabet(temtchar);
        var line = CheckLine(temtchar);
        if (!Digit && !Alphabet && !line) {
            alert("登录名必须是由字母、数字、下划线等组成!");
            return true;
        }
    }
    return false;
}

//------密码必须是由字母、数字、下划线组成!------------
function notValidPassWord(obj)
{
    var objValue = obj.value;
    for (var i = 0; i < objValue.length; i++) {
        var temtchar = objValue.charAt(i);
        //  alert(temtchar+":"+temtchar.charCodeAt(0));
        var Digit = CheckDigit(temtchar);
        var Alphabet = CheckAlphabet(temtchar);
        var line = CheckLine(temtchar);
        if (!Digit && !Alphabet && !line) {
            alert("密码必须是由字母、数字、下划线组成!");
            return true;
        }
    }
    return false;
}

function CheckDigit(DigitStr) {
    var flag = true;
    var Digitlength = DigitStr.length;
    for (var i = 0; i < Digitlength; i++) {
        if (DigitStr.charCodeAt(i) < 48 || DigitStr.charCodeAt(i) > 57) {
            flag = false;
            break;
        }
    }

    return flag;
}

function convertToHtml(str)
{
    var re;
    re = / /g;
    str = str.replace(re, "&nbsp;");
    re = /\r\n/g;
    str = str.replace(re, "<br>");
    return str;
}

function makeToHtml(formName)
{
    var form;
    form = eval(formName);
    for (i = 0; i < form.elements.length; i++)
        if (form.elements[i].type == "textarea")
            form.elements[i].value = convertToHtml(form.elements[i].value);
}


//------打印-------
function printinfo() {

    form1.target = "_blank";
    var action = form1.action;
    if (form1.printflag = "undefine") {
        test = action;
        if (test.indexOf("BeanAddServlet") > 0) {
            alert("请保存信息后再使用打印功能");
            form1.target = "_self";
            return;
        }
        var a = test.indexOf("Update");
        if (a > 0)  test = test.substring(0, a) + "DisplayServlet" + test.substring(a + 13);
        //alert(test);
        if (test.indexOf("?") > 0)
            form1.action = test + "&printflag=1";
        else
            form1.action = test + "?printflag=1";
    } else {
        form1.all.printflag.value = "1";
    }
    form1.submit();
    form1.action = action;
    form1.target = "_self";
}

//---------修改后提示是否返回列表 ------------
function confirmPage(strurl) {
    if (confirm("保存成功！选择'确定'可返回列表;")) {
        window.open(strurl, '_self');
    }
}


function isCharsInBag(s, bag)
{
    var i,c;
    for (i = 0; i < s.length; i++)
    {
        c = s.charAt(i);
        //字符串s中的字符
        if (bag.indexOf(c) > -1)
            return c;
    }
    return "";
}
//-------检查函数:中文---------------
function ischinese(name)
{

    var errorChar;
    var badChar = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789><,[]{}?/+=|\\'\":;~!#$%()`";
    errorChar = isCharsInBag(name.value, badChar)
    if (errorChar != "")
    {
        alert("请重新输入中文\n");
        name.focus();
        return true;
    }

    return false;
}
//----------检查函数: 中文或数字-------------
function ischineseordata(name)
{

    var errorChar;
    var badChar = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz><,[]{}?/+=|\\'\":;~!#$%()`";
    errorChar = isCharsInBag(name.value, badChar)
    if (errorChar != "")
    {
        alert("请重新输入中文或数字\n");
        name.focus();
        return true;
    }

    return false;
}


//===============================================================================================================================

function notNumber1(name,str){//是否数字 by lyj
  if(trim(name.value)!=""){
      if(isNaN(trim(name.value))){
        alert(str);
        name.focus();
       return true;
     }
  }
  return false;
}

function notNY(name,str){//是否合法年月 by lyj
  if(trim(name.value)==""||trim(name.value).length!=6||isNaN(trim(name.value))){
    alert(str);
    name.focus();
    return true;
  }
  if(trim(name.value).substring(4,6)>12){
    alert("月份不能大于12,请核对!");
     name.focus();
     return true;
  }
  return false;
}
function deleteID(str,url){
	 if(confirm("确定删除“"+str+"”吗？")){
         location.href=url;
     } 
}




